package com.cblz.bd.server.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cblz.bd.common.core.entity.BaSecurityDataRecycle;
import com.cblz.bd.common.core.exception.BdAdminException;
import com.cblz.bd.common.core.utils.ResultResponse;
import com.cblz.bd.common.core.utils.SearchParamsUtil;
import com.cblz.bd.server.core.annotation.AdminApiController;
import com.cblz.bd.server.core.annotation.Log;
import com.cblz.bd.server.core.service.BaSecurityDataRecycleService;
import io.vavr.Tuple2;
import io.vavr.Tuple3;
import io.vavr.Tuple5;
import io.vavr.control.Option;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;


@RequiredArgsConstructor
@AdminApiController("security.dataRecycle")
public class BaSecurityDataRecycleController {

    private final BaSecurityDataRecycleService baSecurityDataRecycleService;

    @PostMapping("index")
    public ResultResponse index(@RequestBody(required = false) Map<String, Object> searchParams) {
        Tuple5 tuple5 = SearchParamsUtil.searchParamsHandle2(searchParams);
        Tuple3<Page<BaSecurityDataRecycle>, QueryWrapper<BaSecurityDataRecycle>, Object> wrapperObjectTuple3 = SearchParamsUtil.params2QueryWrapper(new BaSecurityDataRecycle(), tuple5);

        return new ResultResponse().data(baSecurityDataRecycleService.index(wrapperObjectTuple3));
    }

    /**
     * 添加和修改请求前查询
     *
     * @return
     * @throws Exception
     */
    @GetMapping("add")
    public ResultResponse addBefore() throws Exception {
        Tuple2<Map<String, String>, Map<String, String>> mapMapTuple2 = baSecurityDataRecycleService.queryBeforeAnOperation();

        return new ResultResponse().data(Map.of("tables", mapMapTuple2._1, "controllers", mapMapTuple2._2));
    }

    @PostMapping("add")
    @Log(description = "新增数据安全回收配置")
    public void add(@RequestBody BaSecurityDataRecycle baSecurityDataRecycle) {
        baSecurityDataRecycle.setCreatetime(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        baSecurityDataRecycleService.save(baSecurityDataRecycle);
    }

    @GetMapping("edit")
    public ResultResponse editBefore(@RequestParam Integer id) {
        BaSecurityDataRecycle baSecurityDataRecycle = Option.of(baSecurityDataRecycleService.getById(id)).getOrElseThrow(() -> new BdAdminException("未找到ID为" + id + "数据"));
        return new ResultResponse().data(baSecurityDataRecycle);
    }

    @PostMapping("edit")
    @Log(description = "修改数据安全回收配置")
    public void edit(@RequestBody BaSecurityDataRecycle baSecurityDataRecycle) {
        baSecurityDataRecycle.setUpdatetime(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        baSecurityDataRecycleService.updateById(baSecurityDataRecycle);
    }

    @GetMapping("getTablePk")
    public ResultResponse getPRKByTableName(@RequestParam String table) {
        return new ResultResponse().data(Map.of("pk", this.baSecurityDataRecycleService.getPrimaryKeyString(table)));
    }

    @DeleteMapping("del")
    @Log(description = "删除数据安全回收配置")
    public void del(@RequestBody Map ids) {
        this.baSecurityDataRecycleService.removeByIds((List) ids.get("ids"));
    }
}
